<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 1280692 - sendBeacon() should follow 30x redirect</title>
  <!-- Including SimpleTest.js so we can use waitForExplicitFinish !-->
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
  <p id="display"></p>
  <div id="content" style="visibility: hidden">
    <iframe style="width:100%;" id="testframe"></iframe>
  </div>

<script class="testbody" type="text/javascript">

/* Description of the test:
 *   We do perform a non simple sendBeacon request which should not use CORS and should follow
 *   a 30x cross origin redirect, which is allowed by the spec.
 */

SimpleTest.waitForExplicitFinish();

const BEACON_URL = "http://example.com/tests/dom/tests/mochitest/beacon/beacon-redirect-handler.sjs?beacon";

SpecialPowers.pushPrefEnv({'set': [["beacon.enabled", true]]}, runTest);

var intervalID = null;

function queryIfRedirectSucceeded() {
  clearInterval(intervalID);
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "beacon-redirect-handler.sjs?verifyRedirectDidSucceed", true);
  xhr.onload = function() {
    is(xhr.responseText, "green", "SendBeacon should follow cross origin redirects!");
    SimpleTest.finish();
  };
  xhr.onerror = function() {
    ok(false, "xhr request returned error");
    SimpleTest.finish();
  };
  xhr.send();
}

function runTest() {
  var data = new Uint8Array([0,1,2,3]);
  navigator.sendBeacon(BEACON_URL, data);

  // we have to make sure the channel did follow the redirect hence
  // we have to wait for a few seconds before we can query the result.
  interval = 4000;
  if (navigator.appVersion.indexOf("Android") >= 0) {
    // Android Debug on an emulator can be very slow
    interval = 10000;
  }
  intervalID = setInterval(queryIfRedirectSucceeded, interval);
}

</script>
</pre>
</body>
</html>
